package com.imis.module.system.model.converter;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imis.base.constant.ConverterIgnoreConstant;
import com.imis.module.api.model.ro.SysRegisterRO;
import com.imis.module.api.model.ro.SysUserUpdateBasicInformationRO;
import com.imis.module.system.model.po.SysUser;
import com.imis.module.system.model.ro.SysUserUpdateRO;
import com.imis.module.system.model.vo.SysUserChangeVO;
import com.imis.module.system.model.vo.SysUserVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * <p>
 * SysUserConverter<br>
 * 系统用户转换类
 * </p>
 *
 * @author XinLau
 * @version 1.0
 * @since 2020年04月15日 11:54
 */
@Mapper
public interface SysUserConverter {

    /**
     * 系统用户 实例
     */
    SysUserConverter INSTANCE = Mappers.getMapper(SysUserConverter.class);

    /**
     * PO 转 VO
     *
     * @param sysUser
     * @return SysUserVO
     */
    @Mappings({
            @Mapping(target = "token", ignore = true)
    })
    SysUserVO domain2vo(SysUser sysUser);

    /**
     * PO 转 VO
     *
     * @param sysUser
     * @return SysUserChangeVO
     */
    @Mappings({

    })
    SysUserChangeVO domain2Vo(SysUser sysUser);

    /**
     * PO 转 VO
     *
     * @param sysUserList
     * @return List<SysUserVO>
     */
    @Mappings({})
    List<SysUserVO> domain2Vo(List<SysUser> sysUserList);

    /**
     * RO 转 PO
     *
     * @param sysRegisterRo
     * @return SysUser
     */
    @Mappings({
            @Mapping(target = ConverterIgnoreConstant.ID, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.CREATE_BY, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.CREATE_TIME, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.UPDATE_BY, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.UPDATE_TIME, ignore = true),
            @Mapping(target = "salt", ignore = true)
    })
    SysUser ro2Domain(SysRegisterRO sysRegisterRo);

    /**
     * RO 转 PO
     *
     * @param sysUserUpdateRo
     * @return SysUser
     */
    @Mappings({
            @Mapping(target = ConverterIgnoreConstant.ID, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.CREATE_BY, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.CREATE_TIME, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.UPDATE_BY, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.UPDATE_TIME, ignore = true),
            @Mapping(target = "username", ignore = true),
            @Mapping(target = "password", ignore = true),
            @Mapping(target = "salt", ignore = true),
            @Mapping(target = "status", ignore = true),
            @Mapping(target = "delFlag", ignore = true)
    })
    SysUser updateRo2Domain(SysUserUpdateRO sysUserUpdateRo);

    /**
     * 分页PO转分页VO
     *
     * @param sysUserPage
     * @return Page<SysUserVO>
     */
    @Mappings({
            @Mapping(target = ConverterIgnoreConstant.ASCS, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.ASC, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.DESCS, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.DESC, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.OPTIMIZE_COUNT_SQL, ignore = true)
    })
    Page<SysUserVO> pagePo2pageVo(Page<SysUser> sysUserPage);

    /**
     * RO 转 PO
     *
     * @param sysUser
     * @param basicInformationRo
     * @return SysUser
     */
    @Mappings({
            @Mapping(target = "id", source = "sysUser.id"),
            @Mapping(target = "username", source = "sysUser.username"),
            @Mapping(target = "realname", source = "basicInformationRo.realname"),
            @Mapping(target = "password", source = "sysUser.password"),
            @Mapping(target = "salt", source = "sysUser.salt"),
            @Mapping(target = "avatar", source = "basicInformationRo.avatar"),
            @Mapping(target = "birthday", source = "basicInformationRo.birthday"),
            @Mapping(target = "sex", source = "basicInformationRo.sex"),
            @Mapping(target = "email", source = "sysUser.email"),
            @Mapping(target = "phone", source = "sysUser.phone"),
            @Mapping(target = "status", source = "sysUser.status"),
            @Mapping(target = "delFlag", source = "sysUser.delFlag"),
            @Mapping(target = "createBy", source = "sysUser.createBy"),
            @Mapping(target = "createTime", source = "sysUser.createTime"),
            @Mapping(target = "updateBy", source = "sysUser.updateBy"),
            @Mapping(target = "updateTime", source = "sysUser.updateTime")
    })
    SysUser updateRo2Domain(SysUser sysUser, SysUserUpdateBasicInformationRO basicInformationRo);

    /**
     * 将更新对象与数据库对象合并成新的数据库更新对象
     *
     * @param sysUserUpdateRo - 更新对象
     * @param sysUser         - 数据库对象
     */
    @Mappings({
            @Mapping(target = "realname", source = "sysUserUpdateRo.realname"),
            @Mapping(target = "avatar", source = "sysUserUpdateRo.avatar"),
            @Mapping(target = "birthday", source = "sysUserUpdateRo.birthday"),
            @Mapping(target = "sex", source = "sysUserUpdateRo.sex"),
            @Mapping(target = "email", source = "sysUserUpdateRo.email"),
            @Mapping(target = "phone", source = "sysUserUpdateRo.phone"),
            @Mapping(target = ConverterIgnoreConstant.ID, ignore = true),
            @Mapping(target = "username", ignore = true),
            @Mapping(target = "password", ignore = true),
            @Mapping(target = "salt", ignore = true),
            @Mapping(target = ConverterIgnoreConstant.CREATE_BY, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.CREATE_TIME, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.UPDATE_BY, ignore = true),
            @Mapping(target = ConverterIgnoreConstant.UPDATE_TIME, ignore = true),
            @Mapping(target = "status", ignore = true),
            @Mapping(target = "delFlag", ignore = true)
    })
    void getUpdatePo(SysUserUpdateRO sysUserUpdateRo, @MappingTarget SysUser sysUser);

}
